/*
 * @Author: silence
 * @Date: 2023-03-15 10:05:11
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-01-04 11:19:13
 * @Description: 语言设置
 */
import 'package:ems_neutral_app/config/AppInfo.dart';
import 'package:ems_neutral_app/pages/Mine/Language/SilLocalizations/SilLocalizations.dart';
import 'package:ems_neutral_app/utils/router/ARouter.dart';
import 'package:flutter/material.dart';
import '../../../base/SilAppBar.dart';
import '../../../base/SilScaffold.dart';
import '../../../utils/color/ColorUtils.dart';

import 'package:get/instance_manager.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';

import 'LanguageController.dart';

class LanguageSetPage extends StatefulWidget {
  const LanguageSetPage({super.key});

  @override
  State<LanguageSetPage> createState() => _LanguageSetPageState();
}

class _LanguageSetPageState extends State<LanguageSetPage> {
  LanguageController _languageController =
      Get.put<LanguageController>(LanguageController());

  @override
  Widget build(BuildContext context) {
    return SilScaffold(
      sideMargin: 0,
      stateBarHide: true,
      navBarHide: true,
      body: mainWidget(),
    );
  }

  mainWidget() {
    /// 语言名称描述
    List<String> languageDiscList =
        _languageController.languageMap.values.toList();
    List<String> languageList = _languageController.languageMap.keys.toList();

    return Container(
      decoration: BoxDecoration(
          image: DecorationImage(
              image: AssetImage(AppInfo.stationType == '3'
                  ? 'imgs/set/set_add_bg_e3.png'
                  : 'imgs/set/set_add_bg.png'),
              fit: BoxFit.fill)),
      child: ListView.separated(
          padding: EdgeInsets.only(bottom: 20.w),
          itemBuilder: (context, index) {
            if (index == 0) {
              return topNavView();
            }

            String languageValue = languageDiscList[index - 1];
            String language = languageList[index - 1];

            return itemView(
              title: languageValue,
              onTapFunc: () async {
                // 用户设置语言
                await _languageController.settingLanguage(language);
                ARoute.pop(context);
              },
            );
          },
          separatorBuilder: (context, index) {
            return Padding(padding: EdgeInsets.only(top: 15.w));
          },
          itemCount: languageDiscList.length + 1),
    );
  }

  /// 顶部信息
  topNavView() {
    return Container(
      margin: EdgeInsets.only(
          top: MediaQuery.of(context).padding.top, bottom: 30.w),
      child: SilAppBar(
        backgroundColor: Colors.transparent,
        leadingWidget: SilScaffoldButton.backButtonStatic(context),
        titleWidget:
            SilScaffoldButton.titleView(title: LangOf(context)?.mine?.language),
      ),
    );
  }

  itemView({String? title, void Function()? onTapFunc}) {
    return SilEmptyButton(
      onTapFunc: onTapFunc,
      margin: EdgeInsets.symmetric(horizontal: 35.w),
      padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 15.w),
      useConstrained: true,
      borderColor: ColorUtils.hexColor('#767a7f'),
      borderWidth: 0.5.w,
      radius: 8.w,
      backgroundColor: Color.fromRGBO(0, 0, 0, 0.3),
      alignment: Alignment.center,
      child: SilText(
        title,
        color: ColorUtils.color_font_main,
        fontSize: 19.sp,
      ),
    );
  }
}
